// pagesProperty/pages/event/detail/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading:true,
    hasData: false,
    task:{},
    netWork:false,
    taskId:0,
    targetId:0,
    lng:0,
    lat:0,
    isShare:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.taskId = parseInt(options.id)
    }
    if(options.isShare){
      this.data.isShare = parseInt(options.id);
      that.setData({
        isShare: this.data.isShare
      })
    }
  },
  onShow(){
    this.modalcnt();
  },
  
  modalcnt:function  ()  {
    var that = this;
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.lng = latitude//纬度
        that.data.lat = longitude//经度
        that.setData({
          lng : latitude,
          lat : longitude
        })
        app.setStorageSync("lng",longitude);
        app.setStorageSync("lat",latitude);
        that.onHttpInit();
      },fail:function(res){
        that.data.lng = 0
        that.data.lat = 0
        app.setStorageSync("lng",0);
        app.setStorageSync("lat",0);
        that.onHttpInit()
      }
    })
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/plan/task/detail', {
      task_id: that.data.taskId,
      lng: that.data.lng,
      lat: that.data.lat,
      isShare: that.data.isShare,
    }, function (res) {
      console.log("获取任务详情", res)
      if (res.data.code == 0) {
        var task = res.data.data.task;        
        that.setData({
          hasData: true,
          task: task
        })
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  goSurvey(e){
    
    if(this.data.task.isMyTask == 0){
      Toast('暂无操作权限');
      return;
    }

    let surveyId = e.currentTarget.dataset.item.surveyStandardId;
    wx.navigateTo({
      url: '../../survey/question/index?id=' + surveyId,
    })
  },
  scanCode: function (res) {
    var task = res.currentTarget.dataset.task;
    var item = res.currentTarget.dataset.item;
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    var estate_id = app.getStorageSync("estate_id");
    that.data.from_page = 'scancode';
    
    if(this.data.task.isMyTask == 0){
      Toast('暂无操作权限');
      return;
    }
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      if(this.data.isShare == 0){
        wx.scanCode({
          onlyFromCamera: true,
          success: (res) => {
            console.log("扫码结果", res);
            var qrcontent = res.result;
            console.log('扫码结果',qrcontent)
            var json_param = {}
            var linkParam = qrcontent;
            var estate_id = 0;
            var qrtype = '';
            if(qrcontent.indexOf('https://') >= 0){
              var q = "?" + qrcontent.split("?")[1];
              var json_param = common_Js.getQueryVariable(q);
              var linkParam = "qrtype=mer&no=" + json_param.no;
              if(qrcontent.indexOf('mer/pay') >= 0){//商家的线下支付码
                wx.navigateTo({ url: '/pages/pay/merOffline/index?no=' + json_param.no })
              } 
              else if(qrcontent.indexOf('wxmini/terminal') >= 0){//巡检设备
                let surveyId = item.surveyTemplateId;
                if(surveyId == undefined || surveyId == 0){
                  Toast.fail('未绑定标准内容，请联系管理员'); return;
                }
                console.log('json_param.id',json_param.id)
                console.log('item.id',item.id)
                if(parseInt(json_param.id) == item.id){
                  let  taskInfoId = item.taskInfo.id;
                  wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id + '&taskInfoId='+ taskInfoId +'&targetId='+item.id+'&id=' + surveyId })
                }else{
                  Toast.fail('请扫码本任务内的设备二维码')
                }
              } 
              else if(qrcontent.indexOf('wxmini/floor') >= 0){//保洁的楼层
                let surveyId = item.surveyTemplateId;
                if(surveyId == undefined || surveyId == 0){
                  Toast.fail('未绑定标准内容，请联系管理员'); return;
                }
                console.log('json_param.id',json_param.id)
                console.log('item.id',item.floorId)
                if(parseInt(json_param.id) == item.floorId){
                  let taskInfoId = item.taskInfo.id;
                  wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id +'&taskInfoId=' + taskInfoId +'&targetId=' + item.floorId + '&id=' + surveyId })
                }else{
                  Toast.fail('请扫码本任务内的区域二维码')
                }
              } 
              else if(qrcontent.indexOf('wxmini/area') >= 0){//区域
                console.log('wxmini/area  item',item)
                let surveyId = item.cleanArea.surveyTemplateId;
                if(surveyId == undefined || surveyId == 0){
                  Toast.fail('未绑定标准内容，请联系管理员'); return;
                }
                console.log('json_param.id',json_param.id)
                console.log('item.cleanArea',item.cleanArea)
                if(parseInt(json_param.id) == item.cleanArea.id){
                  console.log('item.cleanArea.taskInfo', item.cleanArea.taskInfo)
                  let taskInfoId = item.cleanArea.taskInfo.id;
                  wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id+'&taskInfoId=' + taskInfoId +'&targetId=' + 0 + '&id=' + surveyId });
                }else{
                  Toast.fail('请扫码本任务内的区域二维码');
                }
              } 
              else if(qrcontent.indexOf('wxmini/patrol') >= 0){//巡更点
                let surveyId = item.point.surveyTemplateId;
                if(surveyId == undefined || surveyId == 0){
                  Toast.fail('未绑定标准内容，请联系管理员'); return;
                }
                console.log('json_param.id',json_param.id)
                console.log('item.point.id',item.point.id)
                if(parseInt(json_param.id) == item.point.id){
                  let  taskInfoId = item.point.taskInfo.id;
                  wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id+'&taskInfoId=' + taskInfoId  +'&targetId=' + item.point.id  + '&id=' + surveyId });
                }else{
                  Toast.fail('请扫码本任务内的巡更点二维码')
                }
              }else if(qrcontent.indexOf('wxmini/event') >= 0){//合并点的码
  
                let pointQrId = 0; 
                let pointQrInfoId = 0;
                let eventType = item.eventType;
                
                // 1-巡检   6-维保
                if(eventType == 1 || eventType == 6){
                  pointQrId = item.pointQrId; 
                  pointQrInfoId = item.pointQrInfoId;
                }
                //巡更
                else if(eventType == 2){
                  pointQrId = item.pointQrId; 
                  pointQrInfoId = item.pointQrInfoId;
                } 
                //3-保洁  4-绿化  5-消杀
                else if(eventType == 3 || eventType == 4 || eventType == 5){
                  pointQrId = item.pointQrId; 
                  pointQrInfoId = item.pointQrInfoId;
                }
  
  
                if(pointQrId == undefined || pointQrId == 0){
                  Toast.fail('未绑定扫码点'); return;
                }
                console.log('item',item)
                // console.log('item.point',item.point)
                console.log('json_param.id',json_param.id)
                // console.log('item.point.pointQrId',item.point.pointQrId)
  
                //扫码点
                if(parseInt(json_param.id) == item.pointQrId){
                  let taskInfoId = item.taskInfo.id;
                  wx.navigateTo({ url: '/pagesProperty/pages/survey/questionNew/index?taskId=' + task.id 
                      + '&taskInfoId=' + taskInfoId  +'&pointQrId=' + pointQrId  + '&eventType=' + eventType + '&pointQrInfoId=' + pointQrInfoId })
                }else{
                  Toast.fail('请扫码本任务内的巡更点二维码')
                }
              } else{
                Toast.fail("当前二维码无效");
              }
            }else{
              if (qrcontent.indexOf('qrtype') < 0) {//加密过的数据
                qrcontent = CryptoJS.Decrypt(res.result)//解密
                console.log('解密：qrcontent',qrcontent)
                json_param = JSON.parse(qrcontent);
                qrtype = json_param["qrtype"];
                linkParam = "qrtype=" + json_param["qrtype"];
                if(qrcontent.indexOf('estate') >= 0){
                  linkParam += "&id=" + json_param["id"] + "&estate_id="+ json_param["id"];
                  estate_id = parseInt(json_param["id"]);
                }
              }
              console.log('linkParam',linkParam)
              var is_estate_indexof = qrcontent.indexOf('estate');
              var is_device_indexof = qrcontent.indexOf('device');
              if (is_estate_indexof >= 0) {
                this.estateOp(linkParam,estate_id)
              } else{
                Toast.fail('该二维码不属于该操作');
              }
            }
          },
          fail: (res) => { console.log(res); }
        })
      }
    }
  },
  goShowResult(e){
    var item = e.currentTarget.dataset.item;
    var task_info_id = 0;
    //<!-- // 1-巡检  2-巡更  3-保洁  4-绿化  5-消杀  6-维保 -->
    //task.eventType == 1 || task.eventType == 6
    if(item.eventType == 1 || item.eventType == 6){
      task_info_id = item.taskInfo.id;
    } else if(item.eventType == 2){
      task_info_id = item.point.taskInfo.id;
    } else if(item.eventType == 3|| item.eventType == 4 || item.eventType == 5){
      task_info_id = item.cleanArea.taskInfo.id;
    }
    console.log('item', item)
    wx.navigateTo({
      url: '../result/index?task_info_id=' + task_info_id
    })
  },
  goMap(e){
    var item = e.currentTarget.dataset.item;
    var type = e.currentTarget.dataset.type;
    let title = '';
    if(type == 'terminal'){
      title = item.terminalName
    }else if(type == 'point') {
      title = item.patrolTitle;
    }else if(type == 'areaInfo') {
      title = item.areaName;
    }
    console.log('item',item)
    wx.navigateTo({
      url: '../map/index?type=' + type + '&id=' + item.id+'&lng=' + item.lng+'&lat=' + item.lat +'&title=' + title,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var user_id = app.getStorageSync("user_id");
    console.log("转发成功 user_id",user_id)
    return {
      title: that.data.task.planTitle,
      path: '/pagesProperty/pages/event/detail/index?id=' + that.data.taskId+'&isShare=1',
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }
  },  
})